﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TE3.Utils;

namespace TE3
{
	public class Map : IDrawable
	{
		public TileInfo[,,] MapData;
		public Int2 MapSize;
		public Int2 TileSize;
		public Int2 NeededSize
		{
			get
			{
				return new Int2(MapData.GetLength(0) * TileSize.X, MapData.GetLength(1) * TileSize.Y);
			}
		}
		public Map(Int2 size, int layers, Int2 tileSize)
		{
			MapData = new TileInfo[size.X, size.Y, layers];
			MapSize = new Int2(size.X, size.Y);
			Console.WriteLine("New map initialized, size = " + size.ToString() + ", layercount = " + layers);
			TileSize = tileSize;
		}

		public void FillRect(Int2 pos1, Int2 pos2, int layer, TileInfo tileInfo)
		{
			for(var x = pos1.X; x < pos2.X; x++)
			{
				for(var y = pos1.Y; y < pos2.Y; y++)
				{
					MapData[x, y, layer] = tileInfo;
				}
			}
		}

		public void Draw(SpriteBatch spriteBatch, GraphicsDevice device, TextureHandler handler)
		{
			for (var x = 0; x < MapData.GetLength(0); x++)
			{
				for (var y = 0; y < MapData.GetLength(1); y++)
				{
					for (var l = 0; l < MapData.GetLength(2); l++)
					{
						var cInfo = MapData[x, y, l];
						if(cInfo.TextureName != null)
							spriteBatch.Draw(handler.Textures[cInfo.TextureName],new Rectangle(x * TileSize.X, y * TileSize.Y, TileSize.X, TileSize.Y),Color.White);
					}
				}
			}
		}
	}
	public struct TileInfo
	{
		public string TextureName;
		public bool IsSolid;
		public TileInfo(string textureName, bool isSolid = true)
		{
			TextureName = textureName;
			IsSolid = isSolid;
		}
	}

}
